package com.itlong.cloud.controller.old;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.app.old.AppOldUserInfoDto;
import com.itlong.cloud.POJO.VO.app.AppUserOwnProjectVO;
import com.itlong.cloud.common.network.tcp.mina.IMinaServiceClient;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.equip.app.IAppOldDeviceRecordServiceClient;
import com.itlong.cloud.project.app.IAppProjectServiceClient;
import com.itlong.cloud.project.app.IAppRoomServiceClient;
import com.itlong.cloud.project.operate.IOperateProjectServiceClient;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.user.app.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <desc>
 *     旧版APP我的版块控制器
 * </desc>
 *
 * Created 2017/10/11
 */
@RestController
@RequestMapping("/mobile/interface")
public class AppOldMineController {
    private static final Logger LOG = LoggerFactory.getLogger(AppOldMineController.class);
    @Autowired
    RedisService redisService;

    @Autowired
    IAppOldMineServiceClient iAppOldMineServiceClient;

    @Autowired
    IAppOldRegisterServiceClient iAppOldRegisterServiceClient;

    @Autowired
    IAppOldDeviceRecordServiceClient iAppOldDeviceRecordServiceClient;

    @Autowired
    IMinaServiceClient iMinaServiceClient;

    @Autowired
    IOperateProjectServiceClient iOperateProjectServiceClient;

    @Autowired
    IAppRoomServiceClient iAppRoomServiceClient;

    @Autowired
    IAppMineServiceClient iAppMineServiceClient;

    @Autowired
    IAppOldHomeServiceClient iAppOldHomeServiceClient;

    @Autowired
    IAppRegisterServiceClient iAppRegisterServiceClient;

    @Autowired
    IAppProjectServiceClient iAppProjectServiceClient;
    /**
     * <desc>
     *     旧版APP_邀请家人查询当前用户的业主房间信息
     * </desc>
     * @param projectId
     * @param phoneNo
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/authPerson/ownerUserInfo")
    public Message getUserOwnerAddress(String projectId, String phoneNo) throws Exception {
        if(StringUtils.isBlank(phoneNo)) {
            return new Message(Code.NOT_PARAM);
        }
        /*AppOldUserInfoVO appOldUserInfoVO = (AppOldUserInfoVO)redisService.getObject(RedisConstant.OLD_APP_USER_VO_KEY+phoneNo,AppOldUserInfoVO.class);
        if (appOldUserInfoVO == null){
            return new Message(Code.TOKEN_ERR);
        }*/
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto == null){
            return new Message(Code.TOKEN_ERR);
        }
        if (appOldUserInfoDto.getUserStatus() != 1){
            return new Message(Code.USER_STATUSERR);
        }
        JSONArray projectArr = new JSONArray();
        JSONObject projectJS = iAppOldMineServiceClient.getUserOwnRoom(appOldUserInfoDto.getUserId(),projectId);
        projectArr.add(projectJS);
        return new Message(Code.SUCCESS,projectArr);
    }

    /**
     * <desc>
     *     旧版APP_邀请家人提交
     * </desc>
     * @param phoneNo
     * @param authPhoneNo
     * @param userRemarks
     * @param projectId
     * @param buildIds
     * @param unitIds
     * @param roomNums
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/authPerson/add")
    public Message appAdd(String phoneNo,String authPhoneNo,String userRemarks,String projectId,String buildIds,String unitIds,String roomNums) throws Exception {
        return new Message(Code.APP_UPDATE);
    }

    /**
     * <desc>
     *     旧版APP_删除授权人
     * </desc>
     * @param phoneNo
     * @param authPhoneNo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/authPerson/delete")
    public Message appDelete(String phoneNo,String authPhoneNo,String projectId) throws Exception {
        /*if (StringUtils.isBlank(phoneNo) || StringUtils.isBlank(authPhoneNo) || StringUtils.isBlank(projectId)) {
            return new Message(Code.NOT_PARAM);
        }
        Message message = iAppOldMineServiceClient.delUserChild(phoneNo,authPhoneNo,projectId);
        return  message;*/
        return new Message(Code.APP_UPDATE);
    }

    /**
     * <desc>
     *     旧版APP_已授权家人列表
     * </desc>
     * @param projectId
     * @param phoneNo
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/authPerson/list")
    public Message myAuthPersionList(String projectId,String phoneNo,Integer pageNo,Integer pageSize) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = (AppOldUserInfoDto)redisService.getObject(RedisConstant.OLD_APP_LOGIN_KEY+phoneNo,AppOldUserInfoDto.class);
        if (appOldUserInfoDto == null){
            appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        }
        if (appOldUserInfoDto == null) {
            return new Message(Code.TOKEN_ERR);
        }
        if (appOldUserInfoDto.getUserStatus() != 1){
            return new Message(Code.USER_STATUSERR);
        }
        Message message = iAppOldMineServiceClient.getAuthPersionList(projectId,appOldUserInfoDto.getUserId(),pageNo,pageSize);
        return  message;
    }

    /**
     * <desc>
     *     旧版APP_修改授权人备注
     * </desc>
     * @param phoneNo
     * @param authPhoneNo
     * @param userRemarks
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/authPerson/update")
    public Message appUpdate(String phoneNo,String authPhoneNo,String userRemarks) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = (AppOldUserInfoDto)redisService.getObject(RedisConstant.OLD_APP_LOGIN_KEY+phoneNo,AppOldUserInfoDto.class);
        if (appOldUserInfoDto == null){
            appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        }
        if (appOldUserInfoDto == null) {
            return new Message(Code.TOKEN_ERR);
        }
        if (appOldUserInfoDto.getUserStatus() != 1){
            return new Message(Code.USER_STATUSERR);
        }
        Message message = iAppOldMineServiceClient.updateAuthUserRemark(appOldUserInfoDto.getUserId(),authPhoneNo,userRemarks);
        return  message;
    }

    /**
     * <desc>
     *     旧版APP_添加用户反馈信息
     * </desc>
     * @param feedbackInfo
     * @param phoneNo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/feekBack/add")
    public Message appAdd(String feedbackInfo,String phoneNo,String projectId) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = (AppOldUserInfoDto)redisService.getObject(RedisConstant.OLD_APP_LOGIN_KEY+phoneNo,AppOldUserInfoDto.class);
        if (appOldUserInfoDto == null){
            appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        }
        if (appOldUserInfoDto == null) {
            return new Message(Code.TOKEN_ERR);
        }
        if (appOldUserInfoDto.getUserStatus() != 1){
            return new Message(Code.USER_STATUSERR);
        }
        Message message = iAppOldMineServiceClient.addFeekBack(appOldUserInfoDto.getUserId(),projectId,feedbackInfo);
        return  message;
    }

    /**
     * <desc>
     *     旧版APP_查询通行记录
     * </desc>
     * @param phoneNo 当前用户手机号
     * @param authPhoneNo  家人用户的手机号
     * @param pageNo
     * @param pageSize
     * @param projectId 当前项目编号
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/deviceRecord/list")
    public Message appList(String phoneNo,String authPhoneNo,Integer pageNo,Integer pageSize,String projectId) throws Exception{
        if (StringUtils.isNotEmpty(authPhoneNo)){
            phoneNo = authPhoneNo;
        }
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto == null){
            return new Message(Code.NOT_FOUNDUSER);
        }
        if (appOldUserInfoDto.getUserStatus() != 1){
            return new Message(Code.USER_STATUSERR);
        }
        Message message = iAppOldDeviceRecordServiceClient.getDeviceRecord(appOldUserInfoDto.getUserId(),pageNo,pageSize,projectId);
        return  message;
    }

    /**
     * <desc>
     *     旧版APP_远程开门,发送消息到一卡通
     * </desc>
     * @param projectId
     * @param auth
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/deviceAuth/authDevice")
    public Message appAuthDevice(String projectId,String auth) throws Exception{
        JSONObject msg = new JSONObject();
        msg.put("auth", auth);
        Code c = iMinaServiceClient.oldSendMessage(projectId, msg.toJSONString());
        return new Message(c);
    }

    /**
     * <desc>
     *     旧版APP_上传通行记录
     * </desc>
     * @param phoneNo
     * @param recordJson
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/deviceRecord/upload")
    public Message deviceRecordUpload(String phoneNo,String recordJson) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = (AppOldUserInfoDto)redisService.getObject(RedisConstant.OLD_APP_LOGIN_KEY+phoneNo,AppOldUserInfoDto.class);
        if (appOldUserInfoDto == null||StringUtils.isBlank(appOldUserInfoDto.getUserId())){
            appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        }
        if (appOldUserInfoDto == null){
            return new Message(Code.NOT_FOUNDUSER);
        }
        if (appOldUserInfoDto.getUserStatus() != 1){
            return new Message(Code.USER_STATUSERR);
        }
        Message message = iAppOldDeviceRecordServiceClient.deviceRecordUpload(appOldUserInfoDto.getUserId(),recordJson);
        return  message;
    }


    /**
     * <desc>
     *     旧版APP_获取所有正在服务中的小区信息，用户所拥有小区除外
     * </desc>
     * @param phoneNo
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/projectInfo/list")
    public Message getAllProjectInfo(String phoneNo) throws Exception{

        Message message = iAppOldMineServiceClient.getServiceProject(phoneNo);
        return  message;
    }

    /**
     * <desc>
     *     旧版APP_用户添加小区
     * </desc>
     * @param phoneNo
     * @param projectId
     * @param projectName
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/userAddProject")
    public Message addProjectByUser(String phoneNo,String projectId,String projectName) throws Exception{
        /*Message message = iAppOldMineServiceClient.addProjectByUser(phoneNo,projectId,projectName);
        return  message;*/
        return new Message(Code.APP_UPDATE);
    }

    /**
     * <desc>
     *     旧版APP_删除小区
     * </desc>
     * @param phoneNo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/userDelProject")
    public Message  delProjectByUser(String phoneNo,String projectId) throws Exception{
        /*if (StringUtils.isBlank(phoneNo)||StringUtils.isBlank(projectId)){
            return new Message(Code.NOT_PARAM,Code.NOT_PARAM.getMsg());
        }
        iAppOldMineServiceClient.delProjectByUser(phoneNo,projectId);
        //删除用户授权关系
        AppOldUserInfoDto appOldUserInfoDto = (AppOldUserInfoDto)redisService.getObject(RedisConstant.OLD_APP_LOGIN_KEY+phoneNo,AppOldUserInfoDto.class);
        //需要查找用户在当前项目所有的房间，更新状态为0；业主角色的房间下授权关系暂不处理
        boolean isSuccess = iAppMineServiceClient.deleteAuthPerson(appOldUserInfoDto.getUserId(),appOldUserInfoDto.getUserPhone(),appOldUserInfoDto.getCurrentProjectId());
        //获取用户项目信息
        Message message = iAppOldHomeServiceClient.getUserInfo(appOldUserInfoDto.getUserPhone(), projectId, "", "", "");
        return  message ;*/
        return new Message(Code.APP_UPDATE);
    }

    /**
     * <desc>
     *     旧版APP_用户点击切换小区
     * </desc>
     * @param phoneNo
     * @param projectId
     * @param projectName
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/userChanegProject")
    public Message userChangeProject(String phoneNo,String projectId,String projectName) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = (AppOldUserInfoDto)redisService.getObject(RedisConstant.OLD_APP_LOGIN_KEY+phoneNo,AppOldUserInfoDto.class);
        if (appOldUserInfoDto == null){
            appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        }
        List<AppUserOwnProjectVO> userOwnProjectList = iAppRegisterServiceClient.getUserOwnProjectsInfo(appOldUserInfoDto.getUserId(),"-1");
        if(userOwnProjectList!=null && userOwnProjectList.size()>0){
            appOldUserInfoDto.setCurrentProjectId(userOwnProjectList.get(0).getProjectId());
            appOldUserInfoDto.setCurrentProjectName(userOwnProjectList.get(0).getProjectName());
        }
        for(AppUserOwnProjectVO project : userOwnProjectList){
            if(project.getProjectId().equals(projectId)){
                appOldUserInfoDto.setCurrentProjectId(project.getProjectId());
                appOldUserInfoDto.setCurrentProjectName(project.getProjectName());
                break;
            }
        }
        //当前用户所在项目
        iAppProjectServiceClient.setCurrentProject(appOldUserInfoDto.getUserId(),appOldUserInfoDto.getCurrentProjectId(),appOldUserInfoDto.getCurrentProjectName());
        return  new Message(Code.SUCCESS,appOldUserInfoDto);
    }

    /**
     * <desc>
     *     旧版APP_运行日志上传
     * </desc>
     * @param logJson
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/runLog/upload")
    public Message appUpload(String logJson) throws Exception{
        Message message = iAppOldMineServiceClient.addRunLog(logJson);

        return  message;
    }





























}
